{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5d66de4e-97b2-46e6-8ae0-f947922456b3",
   "metadata": {},
   "source": [
    "# 如何加载使用tool\n",
    "- 加载预制tool的方法\n",
    "- 几种tool的使用方式"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11a7758c-5877-4d2c-872d-5b8bcfbead79",
   "metadata": {},
   "source": [
    "langchain预制了大量的tools，基本这些工具能满足大部分需求，https://github.com/langchain-ai/langchain/tree/v0.0.352/docs/docs/integrations/tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9750518-1018-4776-a704-42844c2e11be",
   "metadata": {},
   "outputs": [],
   "source": [
    "#添加预制工具的方法很简单\n",
    "from langchain.agents import load_tools\n",
    "tool_names = [...]\n",
    "tools = load_tools(tool_names) #使用load方法\n",
    "\n",
    "#有些tool需要单独设置llm\n",
    "from langchain.agents import load_tools\n",
    "tool_names = [...]\n",
    "llm = ...\n",
    "tools = load_tools(tool_names, llm=llm) #在load的时候指定llm"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfe1f688-f9f6-4ff8-95b9-b31161fb80f7",
   "metadata": {},
   "source": [
    "## SerpAPI\n",
    "最常见的聚合搜索引擎 https://serper.dev/dashboard"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7a31310-ae74-4424-bbb4-afacbd0ebc77",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.utilities import SerpAPIWrapper\n",
    "\n",
    "#serpapi的api key\n",
    "import os\n",
    "os.environ[\"SERPAPI_API_KEY\"] = \"f265b8d9834ed7692cba6db6618e2a8a9b24ed6964c457296a2626026e8ed594\"\n",
    "\n",
    "search = SerpAPIWrapper()\n",
    "\n",
    "search.run(\"Obama's first name?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72b8fd2b-4a5d-43f9-a4ad-6be5dbb3dfef",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 支持自定义参数，比如将引擎切换到bing，设置搜索语言等\n",
    "params = {\n",
    "    \"engine\": \"bing\",\n",
    "    \"gl\": \"us\",\n",
    "    \"hl\": \"en\",\n",
    "}\n",
    "search = SerpAPIWrapper(params=params)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efb569fc-b165-4db4-8a58-d67afb9dd17d",
   "metadata": {},
   "source": [
    "## 使用Dall-E\n",
    "Dall-E是openai出品的文到图AI大模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ca4663d-aa77-45e5-84db-2c66a2694133",
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install opencv-python scikit-image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0dc6fc02-a0c9-4bad-8ecd-25011a8dedbb",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chat_models import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "    temperature=0,\n",
    "    model=\"gpt-4\",\n",
    ")\n",
    "from langchain.agents import initialize_agent, load_tools\n",
    "\n",
    "tools = load_tools([\"dalle-image-generator\"])\n",
    "agent = initialize_agent(\n",
    "    tools, \n",
    "    llm, \n",
    "    agent=\"zero-shot-react-description\",\n",
    "    verbose=True\n",
    ")\n",
    "output = agent.run(\"Create an image of a halloween night at a haunted museum\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b88f854b-fd95-4084-8c06-65004154030d",
   "metadata": {},
   "source": [
    "## Eleven Labs Text2Speech\n",
    "ElevenLabs 是非常优秀的TTS合成API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "542d36ee-401e-4f17-9948-7feb453fa8bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install elevenlabs\n",
    "! pip install --upgrade pydantic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44ba52bc-71c9-4d43-a1e3-5c7a9b952e53",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\\\"ELEVEN_API_KEY\\\"] = \\\"23261e4a3b79697822252a505a169863\\\"\n",
    "\n",
    "from langchain.tools import ElevenLabsText2SpeechTool\n",
    "\n",
    "text_to_speak = \"Hello! 你好! Hola! नमस्ते! Bonjour! こんにちは! مرحبا! 안녕하세요! Ciao! Cześć! Привіт! வணக்கம்!\"\n",
    "\n",
    "tts = ElevenLabsText2SpeechTool(\n",
    "    voice=\"Bella\",\n",
    "    text_to_speak=text_to_speak,\n",
    "    verbose=True\n",
    ")\n",
    "tts.name\n",
    "\n",
    "speech_file = tts.run(text_to_speak)\n",
    "tts.stream_speech(text_to_speak)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (langchain)",
   "language": "python",
   "name": "langchain-env"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
